//
//  QSEnterpriseArchivesViewController.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/28.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSEnterpriseArchivesViewController.h"
#import "QSWebViewController.h"
@interface QSEnterpriseArchivesViewController ()<WKNavigationDelegate, WKUIDelegate,WKScriptMessageHandler>
@property (nonatomic, strong)WKWebView *webView;
@end

@implementation QSEnterpriseArchivesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configWebView];
}

- (void)configWebView{
    self.navigationItem.title = @"一企一档";
    self.webView = [[WKWebView alloc]init];
//    self.webView.frame = [UIScreen mainScreen].bounds;
    
    [self.view addSubview:self.webView];
    //设置js回调的方法
    [[self.webView configuration].userContentController addScriptMessageHandler:self name:@"GoBack"];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(kRealValue(kStatusBarHeight));
        make.bottom.equalTo(self.view);
    }];
    _webView.UIDelegate = self;
    _webView.navigationDelegate = self;
    if (self.strUrl) {
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.strUrl]]];
    }
//    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@#/home?allToken=%@&platform=iOS", KH5Prefix, kCurrentUser.allToken]]]];
    NSLog(@"--- %@", _webView.URL);
    if(@available(iOS 11.0, *)) {
        _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
}
#pragma mark - WKNavigationDelegate
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    [QSAppKeyWindow showIndeterminateHudWithText:@"加载中"];
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [QSAppKeyWindow hideHud];
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{

}
// 接收到服务器跳转请求之后调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{

}
// 在收到响应后，决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{

    NSLog(@"%@",navigationResponse.response.URL.absoluteString);
    //允许跳转
    decisionHandler(WKNavigationResponsePolicyAllow);
    //不允许跳转
    //decisionHandler(WKNavigationResponsePolicyCancel);
}

 - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    NSLog(@"JS调iOS  name : %@    body : %@",message.name,message.body);
    if ([message.name isEqualToString:@"GoBack"]) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark alert弹出框

- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
    UIAlertAction *alertActionCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        // 返回用户选择的信息
        completionHandler();
    }];
    UIAlertAction *alertActionOK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler();
    }];
//    // alert弹出框
//    WSAlertViewController *alterVC =[WSAlertViewController alertControllerWithTitle:message message:nil preferredStyle:UIAlertControllerStyleAlert];
//    [alterVC addAction:alertActionCancel];
//    [alterVC addAction:alertActionOK];
//    [self presentViewController:alterVC animated:YES completion:nil];
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
